//
// Created by 11510 on 2024/7/8.
//

#ifndef REACTOR_INETADDRESS_H
#define REACTOR_INETADDRESS_H
#include <strings.h>
#include <string.h>
#include <arpa/inet.h>
#include <string>

class InetAddress {
private:
    struct sockaddr_in _addr;
public:
    InetAddress(const struct sockaddr_in& addr): _addr(addr){}

    InetAddress(const std::string & ip, unsigned short port){
        ::memset(&_addr,0, sizeof(struct sockaddr_in));
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(port);   /* 主机转网络 */
        _addr.sin_addr.s_addr = inet_addr(ip.c_str());
    }

    ~InetAddress() = default;

    std::string ip() const{
        /* 网络字节序转主机字节序 */
        return std::string(inet_ntoa(_addr.sin_addr));
    }

    unsigned short port() const{
       return ntohs(_addr.sin_port);
    }

    struct sockaddr_in *getInetAddressPtr(){
        return &_addr;
    }
};


#endif //REACTOR_INETADDRESS_H
